package com.grouponmall.service;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.Span;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.grouponmall.model.Deal;

/**
 * 从九月三十号以来，该网站未进行产品添加，因此，目前忽略对其处理
 * 
 * @author xueqiang.mi
 * @since 2010-5-8
 * @modified 2010-10-10
 */
public class RetrieveFromAdilityDeal extends BaseRetrieveService {

  public static final String URL = "http://www.adilitydeal.com/";
  public static final String SOURCE = "Adility";

  @Override
  @SuppressWarnings("serial")
  public List<Deal> extract(String link) throws ParserException {
    List<Deal> deals = new ArrayList<Deal>();
    Deal deal = new Deal();
    deal.setSource(SOURCE);
    deal.setLink(link);

    Parser parser = new Parser(link);
    NodeList list = parser.extractAllNodesThatMatch(new NodeFilter() {
      public boolean accept(Node node) {
        if (node instanceof Div) {
          return true;
        }
        return false;
      }
    });

    for (Node node : list.toNodeArray()) {
      Div div = (Div) node;
      String clazz = div.getAttribute("class");
      if ("city".equals(clazz)) {
        for (Node n : div.getChildren().toNodeArray()) {
          if (n instanceof LinkTag && n.getFirstChild() != null) {
            deal.setCity(n.getFirstChild().getText().trim());
          }
        }
      } else if ("deal-header".equals(clazz)) {
        deal.setTitle(div.getChildrenAsNodeArray()[3].getChildren().toHtml());
      } else if ("deal".equals(clazz)) {
        NodeList nodes = div.getChildren().extractAllNodesThatMatch(new NodeFilter() {
          public boolean accept(Node node) {
            if (node instanceof Span) {
              return true;
            }
            return false;
          }

        }, true);
        for (Node n : nodes.toNodeArray()) {
          Span span = (Span) n;
          String c = span.getAttribute("class");
          if ("value-unit".equals(c)) {
            deal.setRetailValue(span.getChildrenAsNodeArray()[2].getText());
          } else if ("discount-unit".equals(c)) {
            deal.setDiscount(span.getChildrenAsNodeArray()[2].getText());
          } else if ("saved".equals(c)) {
            deal.setPrice(span.getChildrenAsNodeArray()[2].getText());
          } else if ("time".equals(c)) {
            deal.setTimeLeft(extractTime(span));
          }
        }
      } else if ("box img".equals(clazz)) {
        Div d = (Div) node;
        if (d.getChild(1) != null && d.getChild(1) instanceof ImageTag) {
          ImageTag image = (ImageTag) d.getChild(1);
          deal.setImage(image.getAttribute("src"));
        }
      }
    }
    deals.add(deal);
    return deals;
  }

  @SuppressWarnings("serial")
  private String extractTime(Span span) throws ParserException {
    NodeList nodeList = span.getChildren().extractAllNodesThatMatch(new NodeFilter() {
      public boolean accept(Node node) {
        if (node instanceof TextNode) {
          return true;
        }
        return false;
      }

    }, true);
    StringBuilder sb = new StringBuilder();
    for (Node node : nodeList.toNodeArray()) {
      if (!(node.getText().contains("\n")))
        sb.append(node.getText());
    }
    return sb.toString();
  }

  @Override
  @SuppressWarnings("serial")
  public NodeList getExpectedLinks(String link) throws ParserException {
    Parser parser = new Parser(link);
    NodeList list = parser.extractAllNodesThatMatch(new NodeFilter() {
      public boolean accept(Node node) {
        if (!(node instanceof Div)) {
          return false;
        }

        Div div = (Div) node;
        String clazz = div.getAttribute("id");
        if ("header_cities".equals(clazz)) {
          return true;
        }
        return false;
      }
    });
    list = list.extractAllNodesThatMatch(new NodeFilter() {
      public boolean accept(Node node) {
        if (node instanceof LinkTag) {
          LinkTag link = (LinkTag) node;
          if (link.getLink().contains("/america")) {
            return false;
          }
          return true;
        }
        return false;
      }

    }, true);
    return list;
  }

  public String getURL() {
    return URL;
  }
}
